#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import os
from os.path import exists

from ..toolchain_platformio import toolchain_platformio


class SYLIXOS_ARM_target(toolchain_platformio):
    def __init__(self, model=None):
        toolchain_platformio.__init__(self, model)
        self.dlopen_prefix = ""
        self.extension = "so"
        self.platform = 'https://gitee.com/kaikong/platform-sylixos-arm.git'
        self.framework = ''
        self.board = ''
        self.build_flags = ['-UD_GLIBCXX_USE_C99_STDINT_TR1', '-std=c++11', '-DSYLIXOS',
                            '-std=gnu11',
                            '-fPIC',
                            '-D_POSIX_THREADS', '-fno-omit-frame-pointer', '-fmessage-length=0', '-fsigned-char',
                            '-fno-short-enums', '-fno-exceptions', '-fno-rtti', '-shared', ]
        self.exe = 'program.dll'
        self.static = False
        self.libs = ['plc_app']

    def getPlatform(self):
        return self.platform

    def build_json(self):
        json_build = 'build.json'
        if exists(json_build):
            with open(json_build, 'r', encoding='utf-8') as f:
                build = json.load(f)
                build['build'] += 1
                # if build.get('platform') != platform:
                #     build['platform'] = platform
                #     self.CTRInstance.logger.info(_("Platform Changed . Clean Project ... \n"))
                # os.system("runas /user:Administrator rd /s /q src")
                os.system("rd /s /q .platformio")
        else:
            build = {'build': 1}
        build['Compiler'] = self.CTRInstance.GetTarget().getcontent().getCompiler()
        with open(json_build, 'w', encoding='utf-8') as f:
            json.dump(build, f)
        return build['build']
